﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Commonality.Model;
using Commonality.Model.Tray;
using Commonality.DAL.MySQL;
using Separate.Model.EmptyTrayGather;
using Separate.BLL;
using System.Text.RegularExpressions;

namespace WZS_Auto_Classifying_Separate
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm_Load(object sender, EventArgs e)
        {
            MySqlFunction.InitMySQLFunction();
            Tray.TrayInit();
            Parameter.EmptyTrayGatherParameterLoad();
            Parameter.MySQLParameterLoad();
            Parameter.TrayModelElementLoad();
            Parameter.ExcludeLineLoad();
            Parameter.ExcludePnLoad();
            EmptyTrayMapDisplay();
            MySQLParameterDisplay();
            TrayModelElementDisplay();
            ExcludeListLineDisplay();
            ExcludePnDisplay();
        }
        /// <summary>
        /// 显示空Tray映射关系
        /// </summary>
        private void EmptyTrayMapDisplay()
        {
            Storehouse1.SelectedIndex = (int)EmptyTrayMap.Storehouse1;
            Storehouse2.SelectedIndex = (int)EmptyTrayMap.Storehouse2;
            Storehouse3.SelectedIndex = (int)EmptyTrayMap.Storehouse3;
            Storehouse4.SelectedIndex = (int)EmptyTrayMap.Storehouse4;
            Storehouse5.SelectedIndex = (int)EmptyTrayMap.Storehouse5;
            Storehouse6.SelectedIndex = (int)EmptyTrayMap.Storehouse6;
        }
        private void Storehouse_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((sender as ComboBox).Name)
            {
                case "Storehouse1":
                    Parameter.EmptyTrayGatherParameterMap(1, (TrayModel)Storehouse1.SelectedIndex);
                    break;
                case "Storehouse2":
                    Parameter.EmptyTrayGatherParameterMap(2, (TrayModel)Storehouse2.SelectedIndex);
                    break;
                case "Storehouse3":
                    Parameter.EmptyTrayGatherParameterMap(3, (TrayModel)Storehouse3.SelectedIndex);
                    break;
                case "Storehouse4":
                    Parameter.EmptyTrayGatherParameterMap(4, (TrayModel)Storehouse4.SelectedIndex);
                    break;
                case "Storehouse5":
                    Parameter.EmptyTrayGatherParameterMap(5, (TrayModel)Storehouse5.SelectedIndex);
                    break;
                case "Storehouse6":
                    Parameter.EmptyTrayGatherParameterMap(6, (TrayModel)Storehouse6.SelectedIndex);
                    break;
                default:
                    break;
            }

        }

        private void MySQLHost_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void MySQLPassword_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void MySQLHost_TextChanged(object sender, EventArgs e)
        {
            string host = (sender as TextBox).Text;
            Parameter.UpdateMySQLHost(host);
            Log4Net.MsgInfo("修改MySQL主机为：" + host);
        }

        private void MySQLPassword_TextChanged(object sender, EventArgs e)
        {
            string password = (sender as TextBox).Text;
            Parameter.UpdateMySQLPassword(password);
            Log4Net.MsgInfo("修改MySQL连接密码：" + password);
        }
        private void MySQLParameterDisplay()
        {
            MySQLHost.Text = MySqlFunction.MySql.DataSource;
            MySQLPassword.Text = MySqlFunction.MySql.Password;
            UserId.Text = MySqlFunction.MySql.UserId;
        }
        /// <summary>
        /// 从TextBox中收集Model集合元素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="model"></param>
        private void GetModelSetsInTextBox(object sender,ref List<string> model)
        {
            model.Clear();
            string[] data = (sender as TextBox).Lines;
            foreach (var item in data)
            {
                string[] str = Regex.Split(item, "\\s+", RegexOptions.IgnoreCase);
                foreach (var item_ in str)
                {
                    if (item_ != "" && item_ != null)
                    {
                        model.Add(item_);
                    }
                }
            }
            ListDeWeight(model, ref model);
        }
        /// <summary>
        /// 将TextBox中的元素收集到集合中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ele"></param>
        private void GetTextBoxList(object sender,ref List<string> ele)
        {
            ele.Clear();
            string[] data = (sender as TextBox).Lines;
            foreach (var item in data)
            {
                string[] str = Regex.Split(item, "\\s+", RegexOptions.IgnoreCase);
                foreach (var item_ in str)
                {
                    if (item_ != "" && item_ != null)
                    {
                        ele.Add(item_);
                    }
                }
            }
            ListDeWeight(ele, ref ele);
        }
        /// <summary>
        /// 移除重复元素
        /// </summary>
        /// <param name="inp"></param>
        /// <param name="outp"></param>
        private void ListDeWeight(List<string> inp,ref List<string> outp)
        {
            HashSet<string> data = new HashSet<string>(inp);
            outp.Clear();
            foreach (var item in data)
            {
                outp.Add(item);
            }
        }

        private void UpdateTrayModelElement_TextBox(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13) return;
            switch ((sender as TextBox).Name)
            {
                case "X01TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X01);
                    Parameter.UpdateTrayModelElement(TrayModel.X01, Tray.X01);
                    break;
                case "X03TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X03);
                    Parameter.UpdateTrayModelElement(TrayModel.X03, Tray.X03);
                    break;
                case "X05TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X05);
                    Parameter.UpdateTrayModelElement(TrayModel.X05, Tray.X05);
                    break;
                case "X08TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X08);
                    Parameter.UpdateTrayModelElement(TrayModel.X08, Tray.X08);
                    break;
                case "X13TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X13);
                    Parameter.UpdateTrayModelElement(TrayModel.X13, Tray.X13);
                    break;
                case "X15TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X15);
                    Parameter.UpdateTrayModelElement(TrayModel.X15, Tray.X15);
                    break;
                default:
                    break;
            }
        }
        private void TrayModelElementDisplay()
        {
            foreach (var item in Tray.X01)
            {
                X01TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X03)
            {
                X03TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X05)
            {
                X05TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X08)
            {
                X08TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X13)
            {
                X13TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X15)
            {
                X15TextBox.Text += (item + "\r\n");
            }
        }
        private void ExcludeListLine_CheckedChanged(object sender, EventArgs e)
        {
            byte on = 0;
            if((sender as CheckBox).Checked) { on = 1; } else { on = 0; }
            switch ((sender as CheckBox).Name)
            {
                case "Line1":
                    Parameter.UpdateExcludeLine(1, on);
                    break;
                case "Line2":
                    Parameter.UpdateExcludeLine(2, on);
                    break;
                case "Line3":
                    Parameter.UpdateExcludeLine(3, on);
                    break;
                case "Line4":
                    Parameter.UpdateExcludeLine(4, on);
                    break;
                case "Line5":
                    Parameter.UpdateExcludeLine(5, on);
                    break;
                case "Line6":
                    Parameter.UpdateExcludeLine(6, on);
                    break;
                case "Line7":
                    Parameter.UpdateExcludeLine(7, on);
                    break;
                case "Line8":
                    Parameter.UpdateExcludeLine(8, on);
                    break;
                case "Line9":
                    Parameter.UpdateExcludeLine(9, on);
                    break;
                case "Line10":
                    Parameter.UpdateExcludeLine(10, on);
                    break;
                case "Line11":
                    Parameter.UpdateExcludeLine(11, on);
                    break;
                case "Line12":
                    Parameter.UpdateExcludeLine(12, on);
                    break;
                case "Line13":
                    Parameter.UpdateExcludeLine(13, on);
                    break;
                case "Line14":
                    Parameter.UpdateExcludeLine(14, on);
                    break;
                case "Line15":
                    Parameter.UpdateExcludeLine(15, on);
                    break;
                case "Line16":
                    Parameter.UpdateExcludeLine(16, on);
                    break;
                default:
                    break;
            }
        }
        private void ExcludeListLineDisplay()
        {
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_1 == 0) { Line1.Checked = false; } else { Line1.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_2 == 0) { Line1.Checked = false; } else { Line2.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_3 == 0) { Line1.Checked = false; } else { Line3.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_4 == 0) { Line1.Checked = false; } else { Line4.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_5 == 0) { Line1.Checked = false; } else { Line5.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_6 == 0) { Line1.Checked = false; } else { Line6.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_7 == 0) { Line1.Checked = false; } else { Line7.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_8 == 0) { Line1.Checked = false; } else { Line8.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_9 == 0) { Line1.Checked = false; } else { Line9.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_10 == 0) { Line1.Checked = false; } else { Line10.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_11 == 0) { Line1.Checked = false; } else { Line11.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_12 == 0) { Line1.Checked = false; } else { Line12.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_13 == 0) { Line1.Checked = false; } else { Line13.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_14 == 0) { Line1.Checked = false; } else { Line14.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_15 == 0) { Line1.Checked = false; } else { Line15.Checked = true; }
            if (Separate.BLL.ExcludeList.ExcludeLine.TB1_1FT_16 == 0) { Line1.Checked = false; } else { Line16.Checked = true; }
        }

        private void ExcludePnDisplay()
        {
            foreach (var item in Separate.BLL.ExcludeList.ExcludePn)
            {
                ExcludePnList.Text += (item + "\r\n");
            }
        }

        private void ExcludePnList_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13) return;
            GetTextBoxList(sender, ref Separate.BLL.ExcludeList.ExcludePn);
            Parameter.UpdateExcludePn(Separate.BLL.ExcludeList.ExcludePn);
        }

        private void UserId_TextChanged(object sender, EventArgs e)
        {
            string userId = (sender as TextBox).Text;
            Parameter.UpdateMySQLUserId(userId);
            Log4Net.MsgInfo("UserID：" + userId);
        }
    }
}
